package per.mnxc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import per.mnxc.common.EmailUtil;
import per.mnxc.common.R;
import per.mnxc.common.ValidateCodeUtils;
import per.mnxc.dto.UserDto;
import per.mnxc.pojo.User;
import per.mnxc.service.IUserService;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mnxc
 * @since 2022-11-26
 */

@RestController
@RequestMapping("/user")
@Api("用户相关接口")
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserController {
    @Autowired
    IUserService userService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<IPage<User>> page(int page, int pageSize, String name){
        //构造分页器
        IPage<User> pageInfo = new Page<>(page,pageSize);
        //
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,User::getNickname,name);
        //查询数据
        userService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R<String> register(HttpServletRequest request, @RequestBody UserDto userDto){
        if(userDto==null) return R.error("注册失败");
        //0.检验验证码
        String addr = userDto.getUserEmail();
        String code = String.valueOf(request.getSession().getAttribute(addr));
        if(!code.equals(userDto.getCode())){
            return R.error("验证码错误");
        }
        //1.检测用户相关信息
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserPhone,userDto.getUserPhone());
        User one = userService.getOne(lqw);
        if(one !=null) return R.error("电话号码已被注册");
        //2.用户密码加密
        String password = DigestUtils.md5DigestAsHex(userDto.getUserPWD().getBytes(StandardCharsets.UTF_8));
        userDto.setUserPWD(password);

        //3.添加用户
        userService.save(userDto);
        return R.success("用户注册成功！");
    }

    @ApiOperation("用户验证码接口")
    @PostMapping("/sendMessage")
    public R<String> sendMessage(HttpServletRequest request,@RequestBody User user){
        if(user==null)return R.error("发送验证码失败");
        //1.检验邮箱地址
        if(user.getUserEmail()==null)return R.error("邮箱地址错误");

        //2.生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        //3.记录验证码
        String addr = user.getUserEmail();
        request.getSession().setAttribute(addr,code);
        //4.发送验证码
        EmailUtil.sendEmail("用户注册验证","[MNXC}",addr,code);

        return R.success("验证码发送成功！");
    }

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public R<User> login(HttpServletRequest request,@RequestBody User user){

        return userService.login(request,user);
    }
    @ApiOperation("用户退出接口")
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        return userService.logout(request);
    }
}

